package com.xjrsoft.module.base.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.base.dto.AuthorizeIdsDto;
import com.xjrsoft.module.base.entity.XjrBaseAuthorize;
import com.xjrsoft.module.base.mapper.XjrBaseAuthorizeMapper;
import com.xjrsoft.module.base.service.IXjrBaseAuthorizeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 授权功能表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-11-07
 */
@Service
public class XjrBaseAuthorizeServiceImpl extends ServiceImpl<XjrBaseAuthorizeMapper, XjrBaseAuthorize> implements IXjrBaseAuthorizeService {

    @Override
    public boolean submit(String objectId, Integer objectType, AuthorizeIdsDto authorizeIdsDto) {
        // 删除原有的权限
        List<String> moduleIds = authorizeIdsDto.getModuleIdList();
        List<String> appModuleIds = authorizeIdsDto.getAppModuleIdList();
        Integer[] itemTypes = null;
        if (moduleIds != null) {
            itemTypes = new Integer[] {1, 2, 3, 4,9};
        } else if (appModuleIds != null) {
            itemTypes = new Integer[] {5, 6, 7, 8};
        }
        XjrUser user = SecureUtil.getUser();
        if (user.isAdminUser()) {
            this.remove(Wrappers.<XjrBaseAuthorize>query().lambda()
                    .eq(XjrBaseAuthorize::getObjectId, objectId)
                    .eq(XjrBaseAuthorize::getObjectType, objectType)
                    .in(XjrBaseAuthorize::getItemType, itemTypes));
        } else {
            String[] roleIdArray = StringUtils.split(user.getRoleId(), StringPool.COMMA);
            List<String> roleIds = roleIdArray == null ? null : Arrays.asList(roleIdArray);
            this.baseMapper.deleteAuthsFor(objectId, objectType, SecureUtil.getUserId(), roleIds, itemTypes);
        }

        List<XjrBaseAuthorize> addedList = new ArrayList<>();
        buildAuthorize(objectId, objectType, 1, moduleIds, addedList);


        List<String> buttonIds = authorizeIdsDto.getButtonIdList();
        buildAuthorize(objectId, objectType, 2, buttonIds, addedList);


        List<String> columnIds = authorizeIdsDto.getColumnIdList();
        buildAuthorize(objectId, objectType, 3, columnIds, addedList);


        List<String> formIds = authorizeIdsDto.getFormIdList();
        buildAuthorize(objectId, objectType, 4, formIds, addedList);

        buildAuthorize(objectId, objectType, 5, appModuleIds, addedList);

        List<String> appButtonIds = authorizeIdsDto.getAppButtonIdList();
        buildAuthorize(objectId, objectType, 6, appButtonIds, addedList);

        List<String> appColumnIds = authorizeIdsDto.getAppColumnIdList();
        buildAuthorize(objectId, objectType, 7, appColumnIds, addedList);

        List<String> appFormIds = authorizeIdsDto.getAppFormIdList();
        buildAuthorize(objectId, objectType, 8, appFormIds, addedList);

        // 子系统
        List<String> subSystemIds = authorizeIdsDto.getSubSystemIdList();
        buildAuthorize(objectId, objectType, 9, subSystemIds, addedList);

        if (CollectionUtil.isNotEmpty(addedList)) {
            return this.saveBatch(addedList);
        }
        return true;
    }

    private void buildAuthorize(String objectId, Integer objectType, Integer itemType, List<String> itemIds, List<XjrBaseAuthorize> authorizeList){
        if (CollectionUtil.isNotEmpty(itemIds)) {
            itemIds.forEach(itemId -> {
                XjrBaseAuthorize authorize = new XjrBaseAuthorize();
                authorize.setAuthorizeId(StringUtil.randomUUID());
                authorize.setObjectId(objectId);
                authorize.setObjectType(objectType);
                authorize.setItemId(itemId);
                authorize.setItemType(itemType);
                authorizeList.add(authorize);
            });
        }
    }
}
